<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Poll/classes/PhpFox_ComponentPollVote.class.php, $Author:  $ */



/** Component for voting in poll
 *
 * @version $Id: PhpFox_ComponentPollVote.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.poll
 */
class PhpFox_Mod_Poll_ComponentPollVote extends BaseComponent
{
    function process()
    {
    	App::membersOnly();

        $oPollItem = &$this->_oModule->getItem('Poll');
        /* @var $oPollItem PhpFox_Mod_Poll_ItemPoll */
        $oPollService = &$this->_oModule->getService('Poll');
        /* @var $oPollService PhpFox_Mod_Poll_ServicePoll */
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oTpl = &$this->_oModule->getTemplate();
        $oUrl = &App::getUrl();
        $oReq = &App::getRequest();

        $oSecurityService->checkPerm('poll', 'full');
        $oSecurityService->checkAccess('poll');
        $sUser = $oSecurityService->getCurrentUserLogin();

        $iPollId = $oReq->getInt('id');
        $iAnswerId = $oReq->getInt('answer');
        if (!($iPollId * $iAnswerId))
        {
        	$aCond['id'] = '= ' .  $iPollId . '';
        	$sSort = 'time DESC';
        	list($aItems, $iCnt) = $oPollItem->getList($aCond, 0, 1, $sSort);
        	foreach ($aItems as $iKey => $aItem)
        	{
       	        $aItems[$iKey]['answer'] = $oPollService->isUserCanVote($aItem['id']);
        	    $aItems[$iKey]['can_view'] = (App::isStaff() ? true : ($aItems[$iKey]['answer'] || ($aItem['user'] == $sUser)) ? true : false);
        	    $aItems[$iKey]['color']  = (($iKey % 4 <= 2) && ($iKey % 4 != 0)) ? true : false;
        	}
        	$oTpl->assign(array(
                'bError1' => true,
                'iPollId' => $iPollId,
                'aItem' => (isset($aItems[0]) ? $aItems[0] : '')
            ));
            return $oTpl->fetch('PollVote.html');
        }
        $bError = false;
        if ($oPollService->isUserCanVote($iPollId))
        {
        	$bError = true;
        }
        if (!$bError)
        {
            if (!$oPollService->votePoll($iPollId, $iAnswerId, $sUser))
            {
            	$bError = true;
            }
        }

        if ( !$bError )
        {
			App::gotoUrl('public.poll.view', array('id' => $iPollId));
        }

        $oTpl->assign(array(
            'bError'  => $bError,
            'iPollId' => $iPollId,
        ));
        return $oTpl->fetch('PollVote.html');
    }
}
?>